const app = getApp()
let qCloud = require("../../index.js");

Page({
    /** 页面的初始数据 */
    data: {
        imgPath: qCloud.config.STATIC_IMAGE_PATH,
        tab: 1,

        startTimeDesc: "请选择日期", // 发布日期描述
        startTimeValue: "", // 发布日期
        wagesMonthDesc: "请选择月份", // 工资月份描述
        wagesMonthValue: "", // 工资月份
        searchValue: "",
        produceTaskList: [], // 生产任务列表

        queryTimeDesc: "请选择日期", // 查询时间描述
        queryTimeValue: "", // 查询时间
        approvalList: [], // 审批列表
        upcountList: [], // 上数列表:默认当天数据，根据查询时间可精准查询某日上数数据

        workerUpcountApprovalId: 0, // 修改时，上数审批ID
        throughQuantity: 0, // 修改时，完成数量
        showMark: false, // 是否显示弹出层(修改)
    },

    // 顶部菜单选择
    tabClick(e) {
        let self = this;
        let tab = e.currentTarget.dataset.index;
        self.setData({
            tab: tab // 页签坐标: 1-生产中; 2-已完成;
        });
        if (tab == 1) {
            // 查询员工的上数任务列表
            self.queryStaffUpcountTaskList();
        } else if (tab == 2) {
            // 查询员工的上数列表
            self.queryStaffUpcountList();
        }
    },

    // 搜索
    searchValueInput: function (e) {
        let self = this
        self.setData({
            searchValue: e.detail.value
        })
        self.queryStaffUpcountTaskList();
    },

    /**打开扫码*/
    scanCode: function () {
        let self = this;

        wx.scanCode({
            onlyFromCamera: true,
            scanType: ['barCode', 'qrCode'],
            success: function (res) {
                app.fetchApis(self, qCloud.api.producetask_checkAuthority, {
                    produceTaskId: res.result
                }, "POST", function (e) {
                    if (e.data.code == 200) {
                        if (qCloud.session.getUser().kindType == '4') {
                            wx.navigateTo({
                                url: '/pages/report/upcount?produceTaskId=' + res.result
                            })
                        }
                        if (qCloud.session.getUser().kindType == '2') {
                            wx.navigateTo({
                                url: '/pages/produce/upcount?produceTaskId=' + res.result
                            })
                        }
                    }
                });
            },
            fail(res) {}
        });
    },

    // 发布日期
    bindStartTimeChange(e) {
        let self = this
        let value = e.detail.value
        self.setData({
            startTimeDesc: value,
            startTimeValue: value
        })

        self.queryStaffUpcountTaskList();
    },

    // 工资月份
    bindWagesMonthChange(e) {
        let self = this
        let value = e.detail.value
        self.setData({
            wagesMonthDesc: value,
            wagesMonthValue: value
        })
        self.queryStaffUpcountTaskList();
    },

    // 跳转-上数
    toUpcount(e) {
        let self = this;
        let index = e.currentTarget.dataset.index;
        let produceTask = self.data.produceTaskList[index];
        wx.navigateTo({
            url: "/pages/report/upcount?produceTaskId=" + produceTask.produceTaskId
        });
    },

    // 跳转-详情
    toDetails(e) {
        let self = this;
        let index = e.currentTarget.dataset.index;
        let produceTask = self.data.produceTaskList[index];
        //上报过工序数量后才可以查看详情
        if (produceTask.staffFlag == "1") {
            wx.navigateTo({
                url: "/pages/report/details?produceTaskId=" + produceTask.produceTaskId
            });
        }
    },

    // 查询时间
    bindQueryTimeChange(e) {
        let self = this
        let value = e.detail.value
        self.setData({
            queryTimeDesc: value,
            queryTimeValue: value
        });
        self.queryStaffUpcountList();
    },

    // 修改-完成数量
    throughQuantityInput(e) {
        let self = this
        self.setData({
            throughQuantity: e.detail.value
        });
    },

    // 显示弹出层(修改)
    showModify(e) {
        let self = this
        self.setData({
            workerUpcountApprovalId: e.currentTarget.dataset.id,
            throughQuantity: e.currentTarget.dataset.quantity,
            showMark: true
        });
    },

    // 关闭弹出层(修改)
    closeModify() {
        let self = this
        self.setData({
            workerUpcountApprovalId: 0,
            throughQuantity: 0,
            showMark: false
        });
    },

    // 删除审批记录
    deletedApproval(e) {
        let self = this
        self.setData({
            workerUpcountApprovalId: e.currentTarget.dataset.id,
            throughQuantity: -1
        });
        self.submitStaffApproval();
    },

    /** 生命周期函数--监听页面加载 */
    onLoad: function (options) {
        let self = this;
        self.setData({
            tab: options.tab
        });
        self.setDateTime();

        if (options.tab == 1) {
            // 查询员工的上数任务列表
            self.queryStaffUpcountTaskList();
        } else if (options.tab == 2) {
            // 查询员工的上数列表
            self.queryStaffUpcountList();
        }
    },

    /** 设置日期时间 */
    setDateTime() {
        const timestamp = Date.parse(new Date());
        let dateTime = new Date(timestamp);

        const startTimeY = dateTime.getFullYear();
        let startTimeM = dateTime.getMonth() + 1;
        const startTimeD = dateTime.getDate();
        if (startTimeM < 10) {
            startTimeM = "0" + startTimeM
        }
        const startTime = startTimeY + '-' + startTimeM + '-' + startTimeD;

        let self = this
        self.setData({
            queryTimeDesc: startTime,
            queryTimeValue: startTime
        });
    },

    /** 查询员工的上数任务列表 */
    queryStaffUpcountTaskList: function () {
        let self = this;
        let params = {
            startTime: self.data.startTimeValue, // 发布日期
            wagesMonth: self.data.wagesMonthValue, // 工资月份
            searchValue: self.data.searchValue
        }
        app.fetchApis(self, qCloud.api.workreport_queryStaffUpcountTaskList, params, "POST", function (e) {
            let result = e.data;
            if (result.code == 200) {
                let data = result.data;

                for (let i = 0; i < data.produceTaskList.length; i++) {
                    data.produceTaskList[i].imageUrl = data.produceTaskList[i].imageId == "" ? self.data.imgPath + 'logo.png' : data.produceTaskList[i].imageUrl
                }

                self.setData({
                    produceTaskList: data.produceTaskList
                });
            }
        });
    },

    /** 查询员工的上数列表 */
    queryStaffUpcountList: function () {
        let self = this;
        let params = {
            queryTime: self.data.queryTimeValue // 查询时间
        }
        app.fetchApis(self, qCloud.api.workreport_queryStaffUpcountList, params, "POST", function (e) {
            let result = e.data;
            if (result.code == 200) {
                let data = result.data;

                self.setData({
                    approvalList: data.approvalList,
                    upcountList: data.upcountList
                });
            }
        });
    },

    /** 提交员工的审批数量 */
    submitStaffApproval: function () {
        let self = this;
        let params = {
            workerUpcountApprovalId: self.data.workerUpcountApprovalId,
            throughQuantity: self.data.throughQuantity
        }
        app.fetchApis(self, qCloud.api.workreport_submitStaffApproval, params, "POST", function (e) {
            let result = e.data;
            if (result.code == 200) {
                self.closeModify();
                self.queryStaffUpcountList();
            }
        });
    },

    /** 生命周期函数--监听页面初次渲染完成 */
    onReady: function () {},
    /** 生命周期函数--监听页面显示 */
    onShow: function () {},
    /** 生命周期函数--监听页面隐藏 */
    onHide: function () {},
    /** 生命周期函数--监听页面卸载 */
    onUnload: function () {
        wx.switchTab({
            url: "/pages/index/index",
        })
    },
    /** 页面相关事件处理函数--监听用户下拉动作 */
    onPullDownRefresh: function () {
        let self = this;
        // 查询员工的上数任务列表
        self.queryStaffUpcountTaskList();
    },
    /** 页面上拉触底事件的处理函数 */
    onReachBottom: function () {},
    /** 用户点击右上角分享 */
    onShareAppMessage: function () {}
});
